package com.scalar.chiptrack.operations.finishedGoods.actionCommands;

import com.scalar.chiptrack.operations.finishedGoods.forms.FinishedGoodsEntryForm;
import com.scalar.chiptrack.operations.finishedGoods.EquivalentPartNumber;
import com.scalar.chiptrack.operations.finishedGoods.FinishedGoodsHelper;
import com.scalar.chiptrack.operations.finishedGoods.dao.FinishedGoodsDAO;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.operations.processtypesetup.dao.ProcessTypeSetupDAO;
import com.scalar.chiptrack.ChipTrackExtendedAction;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.ScalarException;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.ArrayList;

/**
 * User: Murali Krishna Yengala
 * Date: May 27, 2005
 * Time: 11:48:49 AM
 */
public class FinishedGoodsNewEntryCommand extends ChipTrackExtendedAction {
    
    public ActionForward executeRequest( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception
    {
        FinishedGoodsEntryForm finishedGoodsEntryForm = ( FinishedGoodsEntryForm ) form;
        HashMap errors              = ( HashMap ) request.getAttribute( ERROR_MESSAGES );
        HashMap info                = ( HashMap ) request.getAttribute( INFO_MESSAGES );
        int baseProductIdI = -1;
        int customerIdI = -1;
        try
        {
            String baseProductIdS = finishedGoodsEntryForm.getBaseProductId();
            baseProductIdS = baseProductIdS == null ? "" : baseProductIdS.trim();
            //String customerIdS = finishedGoodsEntryForm.getCustomerId();
            //customerIdS = customerIdS == null ? "" : customerIdS.trim();
            baseProductIdI = Integer.parseInt( baseProductIdS);
            //customerIdI = Integer.parseInt( customerIdS);
        }
        catch( NumberFormatException e)
        {
            errors.put("Invalid Base Product or Customer selected !", null);
            FinishedGoodsHelper.setBaseProductsAndCustomers( request, errors);
            request.setAttribute( SessionKeys.ERROR_MESSAGES, errors);
            return mapping.findForward("new");
        }
        String[] fgiProcessStepIds = finishedGoodsEntryForm.getFgiProcessStepIds();
        if( fgiProcessStepIds == null || fgiProcessStepIds.length == 0  )
        {
            errors.put("No FGI Product is selected !", null);
            FinishedGoodsHelper.setBaseProductsAndCustomers( request, errors);
            request.setAttribute( SessionKeys.ERROR_MESSAGES, errors);
            return mapping.findForward("new");
        }
        FinishedGoodsHelper.resetFinishedGoodsEntryForm( finishedGoodsEntryForm);        
        finishedGoodsEntryForm.setBaseProductIdI( baseProductIdI);
        finishedGoodsEntryForm.setCustomerIdI( customerIdI);

        finishedGoodsEntryForm.setProcessStepId( null);
        finishedGoodsEntryForm.setProcessStepIdI( -1);

        ArrayList equivalentPartList = new ArrayList();
        equivalentPartList.add(new EquivalentPartNumber());
        finishedGoodsEntryForm.setEquivalentPartNosList( equivalentPartList);
        try
        {
            ArrayList systemParametersList = ProcessTypeSetupDAO.getSystemParameters( finishedGoodsEntryForm.getBaseProcessType());
            finishedGoodsEntryForm.setParametersList( systemParametersList);
        }
        catch( ScalarException e)
        {
            e.printStackTrace();
            errors.put(ERROR_INTERNAL_SERVER_ERROR, "");
        }
        try
        {
            finishedGoodsEntryForm.setVendorList( VendorDAO.getFinishedGoodsLocations());
        }
        catch( ScalarException e)
        {
            e.printStackTrace();
            errors.put(ERROR_INTERNAL_SERVER_ERROR, "");
        }
        //request.setAttribute( "units", ProcessTypeSetupDAO.getUnitsOfMesurment() );
        FinishedGoodsHelper.setFgiProducts(baseProductIdI, request, errors, info);
        FinishedGoodsHelper.setCustomers( request, errors );
        request.setAttribute( "units", ProcessTypeSetupDAO.getUnitsOfMesurment() );
        request.setAttribute( "algorithmList", FinishedGoodsDAO.loadAlgorithmList());
        saveToken( request);
        request.setAttribute( SessionKeys.INFO_MESSAGES, info);
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors);
        return  mapping.findForward("newEntry");
    }
}
